package com.mygdx.game.actor;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

/**
 * 超级马丽 - 马里奥类
 * Created by 80002023 on 2016/8/3.
 */
public class Mario extends Actor {

    float stateTime;

    Texture texturePlayer;
    Texture textureControl;
    TextureRegion currentFrame;

    public ImageButton buttonL;
    public ImageButton buttonR;

    Animation aniRight;
    Animation aniLeft;
    Animation aniIdle;

    STATE state;

    enum STATE {
        LEFT, RIGHT, IDEL
    }

    public Mario(float x, float y) {
        setX(x);
        setY(y);
        this.stateTime = 0;
        show();
        state = STATE.IDEL;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        stateTime += Gdx.graphics.getDeltaTime();

        this.update();

        this.check();

        batch.draw(currentFrame, getX(), getY());

    }

    public void check() {
        if (state == STATE.LEFT) {
            currentFrame = aniLeft.getKeyFrame(stateTime, true);
        } else if (state == STATE.RIGHT) {
            currentFrame = aniRight.getKeyFrame(stateTime, true);
        } else if (state == STATE.IDEL) {
            currentFrame = aniIdle.getKeyFrame(stateTime, true);
        }
    }

    public void update() {

        if (state == STATE.LEFT) {
            setX(getX() - 1.5f);
            if (getX() < 20) setX(20);
        } else if (state == STATE.RIGHT) {
            setX(getX() + 1.5f);
            if (getX() > 400) setX(400);
        }
//        this.x = x;
    }

    public void show() {
        //loading mario image
        texturePlayer = new Texture(Gdx.files.internal("data/Mario.png"));
        TextureRegion[][] mario = TextureRegion.split(texturePlayer, 64, 64);
        TextureRegion[][] mario2 = TextureRegion.split(texturePlayer, 64, 64);
        //reversal image
        for (TextureRegion[] region1 : mario2) {
            for (TextureRegion region2 : region1) {
                region2.flip(true, false);
            }
        }

        //loading mario image
        textureControl = new Texture(Gdx.files.internal("data/btn/control.png"));
        TextureRegion[][] spilt = TextureRegion.split(textureControl, 120, 120);
        TextureRegion[][] spilt2 = TextureRegion.split(textureControl, 120, 120);

        //reversal image
        for (TextureRegion[] region1 : spilt2) {
            for (TextureRegion region2 : region1) {
                region2.flip(true, false);
            }
        }
        //右
        TextureRegion[] regionR = new TextureRegion[3];
        regionR[0] = mario[0][1];
        regionR[1] = mario[0][2];
        regionR[2] = mario[0][0];
        aniRight = new Animation(0.1f, regionR);
        //左
        TextureRegion[] regionL = new TextureRegion[3];
        regionL[0] = mario2[0][1];
        regionL[1] = mario2[0][2];
        regionL[2] = mario2[0][0];
        aniLeft = new Animation(0.1f, regionL);
        //空闲
        TextureRegion[] regionI = new TextureRegion[1];
        regionI[0] = mario[0][0];
//        regionI[1] = mario2[0][0];

        aniIdle = new Animation(0.1f, regionI);

        buttonL = new ImageButton(new TextureRegionDrawable(spilt[0][0]), new TextureRegionDrawable(spilt[0][1]));
        buttonR = new ImageButton(new TextureRegionDrawable(spilt2[0][0]), new TextureRegionDrawable(spilt2[0][1]));
        buttonL.setPosition(20, 20);

//        buttonR.setRotation(180);
        buttonR.setPosition(150, 20);

        buttonL.addListener(new InputListener() {

            @Override
            public void touchUp(InputEvent event, float x, float y,
                                int pointer, int button) {
                state = STATE.IDEL;
                super.touchUp(event, x, y, pointer, button);
            }

            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                                     int pointer, int button) {
                state = STATE.LEFT;
                return true;
            }

        });

        buttonR.addListener(new InputListener() {
            @Override
            public void touchUp(InputEvent event, float x, float y,
                                int pointer, int button) {
                state = STATE.IDEL;
                super.touchUp(event, x, y, pointer, button);
            }

            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                                     int pointer, int button) {
                state = STATE.RIGHT;
                return true;
            }

        });
    }

}
